package net.bwie.realtime.jtp.dws.log.job;


import com.alibaba.fastjson.JSON;
import net.bwie.realtime.jtp.DorisUtil;
import net.bwie.realtime.jtp.KafkaUtil;
import net.bwie.realtime.jtp.dws.log.bean.PageViewBean;
import net.bwie.realtime.jtp.dws.log.function.PageViewBeanMapFunction;
import net.bwie.realtime.jtp.dws.log.function.PageViewWindowFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;


public class JtpTrafficPageViewMinuteWindowDwsJob {
    public static void main(String[] args) throws Exception {
        // 执行环境
        StreamExecutionEnvironment evn = StreamExecutionEnvironment.getExecutionEnvironment();
        evn.setParallelism(1);
        evn.enableCheckpointing(3000L);

        // 数据源
        DataStream<String> PageStream = KafkaUtil.consumerKafka(evn, "dwd-traffic-page-log");

        // 数据转换
        DataStream<String> resultStream = handle(PageStream);
        resultStream.print();

        // 数据输出
        DorisUtil.saveToDoris(
                resultStream,"jtp_realtime_report","dws_traffic_page_view_window_report"
        );

        evn.execute("JtpTrafficPageViewMinuteWindowDwsJob");
    }


    private static DataStream<String> handle(DataStream<String> stream) {
        // 按照 mid 设备id 分组 用于计算uv
        KeyedStream<String, String> midStream = stream.keyBy(
                json -> JSON.parseObject(json).getJSONObject("common").getString("mid")
        );

        DataStream<PageViewBean> beanStream = midStream.map(new PageViewBeanMapFunction());


        DataStream<PageViewBean> timeStream = beanStream.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<PageViewBean>() {
                                    @Override
                                    public long extractTimestamp(PageViewBean pageViewBean, long l) {
                                        return pageViewBean.getTs();
                                    }
                                }
                        )
        );

        KeyedStream<PageViewBean, String> keyedStream = timeStream.keyBy(
                bean -> bean.getBrand() + "," + bean.getChannel() + "," + bean.getProvince() + "," + bean.getIsNew()
        );

        WindowedStream<PageViewBean, String, TimeWindow> windowedStream = keyedStream.window(
                TumblingEventTimeWindows.of(Time.minutes(1))
        );
        SingleOutputStreamOperator<String> reportStream = windowedStream.apply(new PageViewWindowFunction());


        return reportStream;
    }
}
